﻿using Treca.Business.Service;
using Treca.Message.Entity;
using Treca.Util;
using EFCore.Sharding;
using LinqKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Treca.Message.Services
{
    public class MessageInfo : base_message
    {
        public String UserId { get; set; }
        public String IsRead { get; set; }
        public String MessageId { get; set; }
        public DateTime? ReadTime { get; set; }
    }

    public interface IMessageInfoBusiness : ISystemBaseDataBusiness<MessageInfo>
    {

    }

    public class MessageInfoBusiness : IMessageInfoBusiness
    {
        Expression<Func<base_message, base_user_message, MessageInfo>> selectinfo = (a, b) => new MessageInfo
        {
            UserId = b.UserId,
            IsRead = b.IsRead,
            ReadTime = b.ReadTime,
            Id = b.Id,
            MessageId=a.Id

        };
        public IQueryable<MessageInfo> GetIQueryable()
        {
            var selectinfoEx = selectinfo.BuildExtendSelectExpre();
            var service = AutofacHelper.GetScopeService<IRepository>();
            var data = from usermessage in service.GetIQueryable<base_user_message>().AsExpandable()
                       join message in service.GetIQueryable<base_message>() on usermessage.MessageId equals message.Id into a
                       from message in a.DefaultIfEmpty()
                       select selectinfoEx.Invoke(message,usermessage);
            return data;
        }

        public IQueryable<MessageInfo> GetIQueryableAll()
        {
            var selectinfoEx = selectinfo.BuildExtendSelectExpre();
            var service = AutofacHelper.GetScopeService<IRepository>();
            var data = from usermessage in service.GetIQueryable<base_user_message>().AsExpandable()
                       join message in service.GetIQueryable<base_message>() on usermessage.MessageId equals message.Id into a
                       from message in a.DefaultIfEmpty()
                       select selectinfoEx.Invoke(message, usermessage);
            return data;
        }
    }
}
